package pri.hillchen.std.concurrent.chapter5;

import pri.hillchen.std.concurrent.chapter5.util.TestHarness;

import java.util.Random;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

/**
 * @author:hillchen
 * @DESCRIPTION:
 * @Date: Created in 10:54 2018/2/8 0008.
 */
public class FutureTaskDemo {
    private  Random random = new Random();
    private  ProductInfo loadProductInfo() throws LoadDataException{
        try {
            Thread.sleep(100);
            int pid = random.nextInt();
            return new ProductInfo(pid,"pname-"+pid);
        } catch (InterruptedException e) {
            throw new LoadDataException(e);
        }
    }
    class LoadDataException extends RuntimeException{
        public LoadDataException() {
        }

        public LoadDataException(String message) {
            super(message);
        }

        public LoadDataException(String message, Throwable cause) {
            super(message, cause);
        }

        public LoadDataException(Throwable cause) {
            super(cause);
        }

        public LoadDataException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
            super(message, cause, enableSuppression, writableStackTrace);
        }
    }

    class Preloader{
        private final FutureTask<ProductInfo> future = new FutureTask<ProductInfo>(new Callable<ProductInfo>() {
            public ProductInfo call() throws Exception {
                return loadProductInfo();
            }
        });

        private final Thread thread = new Thread(future);

        public void start(){
            thread.start();
        }

        public ProductInfo get() throws InterruptedException {
            try{
                return future.get();
            }catch (ExecutionException e){
                Throwable throwable = e.getCause();
                if(throwable instanceof  LoadDataException){
                    throw (LoadDataException)throwable;
                }else {
                    throw new LoadDataException(throwable);
                }
            }
        }
    }
    class ProductInfo{
        private int pid;
        private String pname;

        public ProductInfo(int pid, String pname) {
            this.pid = pid;
            this.pname = pname;
        }

        @Override
        public String toString() {
            return "ProductInfo{" +
                    "pid=" + pid +
                    ", pname='" + pname + '\'' +
                    '}';
        }
    }

    public Preloader getPreloader(){
        return new Preloader();
    }

    public static void main(String[] args) throws InterruptedException {
        final FutureTaskDemo futureTaskDemo = new FutureTaskDemo();
        Runnable[] runnables = new Runnable[100];
        for(int i=0;i<100;i++){
            runnables[i] = new Runnable() {
                public void run() {
                    try {
                        Preloader preloader = futureTaskDemo.getPreloader();
                        preloader.start();
                        System.out.println(preloader.get());
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            };
        }
        System.out.println("hanlde time:" + new TestHarness().timeTasks(runnables));
    }
}
